# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 评价指标-SC系数.py
# @Author: dongguangwen
# @Date  : 2025-02-15 12:38
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from sklearn.metrics import silhouette_score

# 1.构建数据集
# 创建数据集 1000个样本,每个样本2个特征 4个质心蔟数据标准差[0.4, 0.2, 0.2, 0.2]  簇中心在[-1,-1], [0,0],[1,1], [2,2]
x, y = make_blobs(n_samples=1000, n_features=2, centers=[[-1, -1], [4, 4], [8, 8], [2, 2]], cluster_std=[0.4, 0.2, 0.3, 0.2], random_state=22)

temp_list = []
for clu_num in range(2, 100):
    my_kmeans = KMeans(n_clusters=clu_num, max_iter=100, random_state=22)
    my_kmeans.fit(x)
    ret = my_kmeans.predict(x)
    temp_list.append(silhouette_score(x, ret))  # 获取sc的值


plt.figure(figsize=(18, 8), dpi=100)
plt.xticks(range(0, 100, 3), labels=range(0, 100, 3))
plt.grid()
plt.title("SC")
plt.plot(range(2, 100), temp_list, 'ob-')
plt.show()

"""
通过图像可观察到 n_clusters=4 sse开始下降趋缓, 最佳值4
"""
